package com.usefullc.common.util;

import org.apache.commons.lang3.math.NumberUtils;

import java.time.LocalTime;

/**
 * @author: Sampson
 * @date: 2022-08-09 19:32
 */
public class TimeUtils {


    /**
     *
     * @param time nanosecond 毫微妙数
     * @return
     */
    public static String millSeconds2FormatTime(long time) {
//        time = time / 1000;
        int temp = (int) (time / 1000);
        int hh = temp / 3600;
        int mm = (temp % 3600) / 60;
        int ss = (temp % 3600) % 60;
        int ms = ((int)time % 3600) % 60;
        String result =  (hh < 10 ? ("0" + hh) : hh) + ":" +
                (mm < 10 ? ("0" + mm) : mm) + ":" +
                (ss < 10 ? ("0" + ss) : ss) + "." + ms ;

        return result;
    }

    /**
     *
     * @param time nanosecond 毫微妙数
     * @return
     */
    public static LocalTime millSeconds2LocalTime(long time) {
//        time = time / 1000;
        int temp = (int) (time / 1000);
        int hh = temp / 3600;
        int mm = (temp % 3600) / 60;
        int ss = (temp % 3600) % 60;
        int ms = ((int)time % 3600) % 60;
        String result =  (hh < 10 ? ("0" + hh) : hh) + ":" +
                (mm < 10 ? ("0" + mm) : mm) + ":" +
                (ss < 10 ? ("0" + ss) : ss) + "." + ms ;

        LocalTime localTime = LocalTime.of(hh, mm, ss, ms);
        return localTime;
    }

    public static long formatTime2MillSeconds(String time) {
        String[] times = time.split(":|\\.");
        Long hour = NumberUtils.createLong(times[0]);
        long result = hour * 60  * 60;
        Long minute = NumberUtils.toLong(times[1]);
        result += minute * 60;
        Long second = NumberUtils.toLong(times[2]);
        result += second;
        if(times.length == 4){
            return result * 1000 + NumberUtils.toLong(times[3]);
        }else{
            return result * 1000;
        }
    }
}
